﻿#ifndef VIDEOFRAMEPROVIDER_H
#define VIDEOFRAMEPROVIDER_H

#include <QObject>
#include <QImage>
#include <QThread>
#include <QMutex>
#include <QWaitCondition>
#include <QAtomicInteger>
#include <opencv2/opencv.hpp>

class VideoFrameProvider : public QObject
{
    Q_OBJECT
public:
    explicit VideoFrameProvider(QObject *parent = nullptr);
    ~VideoFrameProvider();

    Q_INVOKABLE void startVideo(const QString &filePath);
    Q_INVOKABLE void stopVideo();
    Q_INVOKABLE void pauseVideo();
    Q_INVOKABLE bool isPlaying() const;
    Q_INVOKABLE void frameProcessed();

signals:
    void newFrameAvailable(const QImage &frame);
    void videoStarted();
    void videoStopped();

private:
    void videoThreadFunc(const QString &filePath);

    cv::VideoCapture m_capture;
    QAtomicInt m_running{0};
    QAtomicInt m_paused{0};
    QThread *m_videoThread = nullptr;
    QMutex m_mutex;
    QWaitCondition m_condition;
    QAtomicInt m_frameInUse{0};
};

#endif // VIDEOFRAMEPROVIDER_H
